package com.itheima.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("pay")
public class PayController {


    /**
     * 预下单：返回二维码的链接，二维码链接生成二维码图片
     * https://openapi.alipay.com/gateway.do   生产环境地址
     * https://openapi.alipaydev.com/gateway.do  沙箱环境地址
     */
    @GetMapping("precreate/{orderNo}")
    public void precreate(@PathVariable Long orderNo) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000121644159",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC6XTOkbtVMyrsET4Wpqq+g4Wt67m7FmD6NQWKJIAsF0k4LuEG+ejM8sFXXZlE3nr6zRCsXzvU6sUZOEr+bArTsRyOk2xIXJUhiGEYE0pDsj3F+l1BHuwIV0NAvB++PeQmpL4dioBnTffLkaJFAMlboW9eB5JBy3BgLOaEMICxCnnPHaJz5x+nRwoQzjTcGmkwvBH4s1UQ76VLoSBvr8VLek2ZL4dr5mDWmDlNTKqOropIQCwt3LTg1toE16ODMHsZDwJH31QikyLWg2nMv5kstL61QnybKNpXccEwos1EGPmC7wCpPlJL0DoUrtI+SMrWTy4GY7MvHLTs2j+lADCYHAgMBAAECggEALQa7pWzJyCXrDV2RM67zg/EyoJ7e0Iq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Gz6xCDLUYw8MxrzSsRlzc6phzCD3/uYcaLnzNWQabegIaoAgutO5xgYIzbCK/rrPRWZbZ0WhXHD6O0QUr3oebmdnG8Jb5AhmSozxYOvGbYH8L2aM1TJppKCV301+qvLjf0=",
                "json",
                "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmUSgBBDCp8jF2nrwxm+24sbjUvkB3ZNs4Up2SKVIN6RDxlKCWcLhrgFyPBxl15Cntt/J6Nr+zHUvQM0rOZZYO1wWPpRlAd+JnhgMA9KoKUOduRrMlKSsPuooxdIdnU6lGYPECitjCMRF8Y28DW2f/WIMz0VPdL5bXG4AXQZrnvCZiL7rSV434fGl0QhJstxekM2IehGj74KSgft2EyLNrKWkL5I2x0W1Rd7yb6YTF1+1SF73XRNx4f9nGKhrNdeDP9CBoPL1gZlvr9vlGrVjSrqq3BRurqQ9GZIQxdhMYmv82VEJbogmsbRsEIONhZX5fT4OQm4IS0599TkIcNZQiQIDAQAB",
                "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");   // 支付结果：回调支付宝-->餐掌柜    主动调用API查询
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo); //交易单号：20210817010101003
        bizContent.put("total_amount", 100000); // 单位元
        bizContent.put("subject", "华为 P999 256G");

        request.setBizContent(bizContent.toString());  // JSON 字符串
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            System.out.println(response.getQrCode());
        } else {
            System.out.println("调用失败");
        }
    }


    /**
     * 查询支付结果
     */
    @GetMapping("query/{orderNo}")
    public AlipayTradeQueryResponse queryOrder(@PathVariable Long orderNo) throws Exception {

        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000121644159",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC6XTOkbtVMyrsET4W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BwU3UoCFJ8gEjnyClqrkAly5oGdXojBASw6ZpMm2FTUD78zXOBaX6H3yGnBz9ir7wJDg+IoAJVaD7Oosn5C2eTOJ8XMlfAYU5kmas6yjCOF3r1M1hA5ZqETgeLnB7tS8oGffLSIYlT7AoGBANNVa4RLnV7N2QYLRvfGyt4xoqn8+WFTbZsNEBieVIwsTy7h6W8MSg/R6GqT54pU7Xd3BSSWDcJmu+h8tDQbJHMKwnz8QcQ0GdtjsYBwW7CUQGrnvvtWuk0UlyHZpzzdhIj9alqwf07v+ES36Bfjcziniq2/Ok0litBmPkg5zoWNAoGAPmn2CguGIExnu8zJkqUYp+9O6cj2DOjEaCEJSKVmHGz6xCDLUYw8MxrzSsRlzc6phzCD3/uYcaLnzNWQabegIaoAgutO5xgYIzbCK/rrPRWZbZ0WhXHD6O0QUr3oebmdnG8Jb5AhmSozxYOvGbYH8L2aM1TJppKCV301+qvLjf0=",
                "json",
                "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmUSgBBDCp8jF2nrwxm+24sbjUvkB3ZNs4Up2SKVIN6RDxlKCWcLhrgFyPBxl15Cntt/J6Nr+zHUvQM0rOZZYO1wWPpRlAd+JnhgMA9KoKUOduRrMlKSsPuooxdIdnU6lGYPECitjCMRF8Y28DW2f/WIMz0VPdL5bXG4AXQZrnvCZiL7rSV434fGl0QhJstxekM2IehGj74KSgft2EyLNrKWkL5I2x0W1Rd7yb6YTF1+1SF73XRNx4f9nGKhrNdeDP9CBoPL1gZlvr9vlGrVjSrqq3BRurqQ9GZIQxdhMYmv82VEJbogmsbRsEIONhZX5fT4OQm4IS0599TkIcNZQiQIDAQAB",
                "RSA2");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo); //交易单号：20210817010101003

        request.setBizContent(bizContent.toString());  // JSON 字符串
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            String tradeStatus = response.getTradeStatus();
            if ("TRADE_SUCCESS".equals(tradeStatus)) {
                System.out.println("支付成功...");
                System.out.println("处理业务逻辑....");
            }
            return response;
        } else {
            System.out.println("调用失败");
        }

        return null;
    }

    /**
     * 退款
     */
    @GetMapping("refund/{orderNo}/{money}/{refundNo}")
    public AlipayTradeRefundResponse refund(@PathVariable Long orderNo, @PathVariable String money, @PathVariable Long refundNo) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000121644159",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC6XTOkbtVMyrsET4Wpqq+g4Wt67m7FmD6NQWKJIAsF0k4LuEG+ejM8sFXXZlE3nr6zRCsXzvU6sUZOEr+bArTsRyOk2xIXJUhiGEYE0pDsj3F+l1BHuwIV0NAvB++PeQmpL4dioBnTffLkaJFAMlboW9eB5JBy3BgLOaEMICxCnnPHaJz5x+nRwoQzjTcGmkwvBH4s1UQ76VLoSBvr8VLek2ZL4dr5mDWmDlNTKqOropIQCwt3LTg1toE16ODMHsZDwJH31QikyLWg2nMv5kstL61QnybKNpXccEwos1EGPmC7wCpPlJL0DoUrtI+SMrWTy4GY7MvHLTs2j+lADCYHAgMBAAECggEALQa7pWzJyCXrDV2RM67zg/EyoJ7e0Iq6AjLb9blyMFPhW1+HMtgfbPhFpScpVEDcXj+IEdaE1UtjaxWIiZEb2UJO0FPFiSYO2PR4Zk3hieYWS7mPQeVi2ZZ71gEa1hUuSlwbX67CzqBNgPXqshH3gWg/Jzjr1XRrsrqXuHBFgUqRq5nuFikLw7IcVqRkEkkFen6/uEAwTTJy1z1citpmXQjdaMN+jHz1IkI48pSQQvduCsEzpbXNN9m02vGPt1TVLiUa9+q/RuzZoVNaXaCqO1PRuYMtZedZZ9Zl05TQGxOdY1Qw/8hIqNU+iehqUup3fohKv0bv7aEGjHt4SLVKkQKBgQDdyqX481FpOPG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jCMRF8Y28DW2f/WIMz0VPdL5bXG4AXQZrnvCZiL7rSV434fGl0QhJstxekM2IehGj74KSgft2EyLNrKWkL5I2x0W1Rd7yb6YTF1+1SF73XRNx4f9nGKhrNdeDP9CBoPL1gZlvr9vlGrVjSrqq3BRurqQ9GZIQxdhMYmv82VEJbogmsbRsEIONhZX5fT4OQm4IS0599TkIcNZQiQIDAQAB",
                "RSA2");

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo); //交易单号：20210817010101003
        bizContent.put("refund_amount", money); //退款金额
        bizContent.put("out_request_no", refundNo); //部分退款
        request.setBizContent(bizContent.toString());  // JSON 字符串
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return response;
        } else {
            System.out.println("调用失败");
        }

        return null;
    }

    /**
     * 退款查询
     */
    @GetMapping("refund/{orderNo}/{refundNo}")
    public AlipayTradeRefundResponse queryrefund(@PathVariable Long orderNo , @PathVariable Long refundNo) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000121644159",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC6XTOkbtVMyrsET4Wpqq+g4Wt67m7FmD6NQWKJIAsF0k4LuEG+ejM8sFXXZlE3nr6zRCsXzvU6sUZOEr+bArTsRyOk2xIXJUhiGEYE0pDsj3F+l1BHuwIV0NAvB++PeQmpL4dioBnTffLkaJFAMlboW9eB5JBy3BgLOaEMICxCnnPHaJz5x+nRwoQzjTcGmkwvBH4s1UQ76VLoSBvr8VLek2ZL4dr5mDWmDlNTKqOropIQCwt3LTg1toE16ODMHsZDwJH31QikyLWg2nMv5kstL61QnybKNpXccEwos1EGPmC7wCpPlJL0DoUrtI+SMrWTy4GY7MvHLTs2j+lADCYHAgMBAAECggEALQa7pWzJyCXrDV2RM67zg/EyoJ7e0I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HGz6xCDLUYw8MxrzSsRlzc6phzCD3/uYcaLnzNWQabegIaoAgutO5xgYIzbCK/rrPRWZbZ0WhXHD6O0QUr3oebmdnG8Jb5AhmSozxYOvGbYH8L2aM1TJppKCV301+qvLjf0=",
                "json",
                "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmUSgBBDCp8jF2nrwxm+24sbjUvkB3ZNs4Up2SKVIN6RDxlKCWcLhrgFyPBxl15Cntt/J6Nr+zHUvQM0rOZZYO1wWPpRlAd+JnhgMA9KoKUOduRrMlKSsPuooxdIdnU6lGYPECitjCMRF8Y28DW2f/WIMz0VPdL5bXG4AXQZrnvCZiL7rSV434fGl0QhJstxekM2IehGj74KSgft2EyLNrKWkL5I2x0W1Rd7yb6YTF1+1SF73XRNx4f9nGKhrNdeDP9CBoPL1gZlvr9vlGrVjSrqq3BRurqQ9GZIQxdhMYmv82VEJbogmsbRsEIONhZX5fT4OQm4IS0599TkIcNZQiQIDAQAB",
                "RSA2");
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo);
        bizContent.put("out_request_no", refundNo);

//// 返回参数选项，按需传入
//JSONArray queryOptions = new JSONArray();
//queryOptions.add("refund_detail_item_list");
//bizContent.put("query_options", queryOptions);

        request.setBizContent(bizContent.toString());
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return null;
    }
}